{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-15T02:39:18.206277Z",
     "start_time": "2025-09-15T02:39:18.178051Z"
    }
   },
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "X = torch.randn(100, 4)\n",
    "# 我们需要给共享层一个名称，以便可以引用它的参数\n",
    "shared = nn.Linear(8, 8)\n",
    "net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    nn.Linear(8, 1))\n",
    "net(X)\n",
    "# 检查参数是否相同\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])\n",
    "net[2].weight.data[0, 0] = 100\n",
    "# 确保它们实际上是同一个对象，而不只是有相同的值\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([True, True, True, True, True, True, True, True])\n",
      "tensor([True, True, True, True, True, True, True, True])\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T02:55:17.205699Z",
     "start_time": "2025-09-15T02:55:17.198018Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(net.state_dict())\n",
    "print(*[(name, parameter) for name, parameter in net.named_parameters()])"
   ],
   "id": "359a6fe87b8d4d72",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('0.weight', Parameter containing:\n",
      "tensor([[-0.3437,  0.1797, -0.1108,  0.0854],\n",
      "        [-0.0482,  0.4006, -0.0211, -0.1434],\n",
      "        [-0.3791, -0.2148,  0.0477,  0.3084],\n",
      "        [ 0.2909,  0.0057,  0.4231,  0.4625],\n",
      "        [-0.1016, -0.3785,  0.0807, -0.0888],\n",
      "        [-0.3637, -0.0308,  0.3941,  0.0235],\n",
      "        [ 0.1689, -0.0782, -0.4242, -0.1435],\n",
      "        [ 0.3629,  0.0256,  0.2546, -0.2881]], requires_grad=True)) ('0.bias', Parameter containing:\n",
      "tensor([-0.3522,  0.2431, -0.1640,  0.2876,  0.0322,  0.0273, -0.4148, -0.2438],\n",
      "       requires_grad=True)) ('2.weight', Parameter containing:\n",
      "tensor([[ 1.0000e+02,  3.2760e-01,  3.5238e-02, -1.6671e-01,  1.7211e-01,\n",
      "         -3.1399e-01,  3.4488e-01,  2.9855e-01],\n",
      "        [ 2.9906e-01, -1.6420e-01,  3.4108e-02,  2.4973e-01,  1.2262e-01,\n",
      "          1.9083e-01,  1.2079e-01, -9.9093e-02],\n",
      "        [ 1.0762e-01,  1.2398e-01,  2.2729e-01,  1.7450e-01, -1.0325e-01,\n",
      "          9.4450e-02, -2.5306e-01,  2.6842e-01],\n",
      "        [ 3.2372e-01, -1.3938e-01,  2.7893e-01,  2.3740e-01, -4.7597e-02,\n",
      "         -2.9590e-01, -8.2610e-02,  7.4203e-02],\n",
      "        [ 3.2461e-01,  3.1503e-02, -3.1865e-01, -1.3456e-01,  1.4730e-01,\n",
      "         -9.5834e-02, -1.2641e-01,  1.8728e-01],\n",
      "        [ 6.3947e-02,  1.8239e-01, -1.0085e-01,  3.1689e-01, -2.3960e-01,\n",
      "          2.5011e-01,  2.8562e-02,  3.2451e-01],\n",
      "        [-1.2831e-01, -2.3069e-01, -1.3527e-01, -1.0341e-03,  2.4735e-01,\n",
      "         -5.8748e-02, -2.8409e-01, -9.3159e-02],\n",
      "        [-7.7562e-02,  1.1068e-02, -3.1304e-01, -5.7405e-02, -1.8221e-01,\n",
      "          2.9791e-01, -2.1631e-01,  3.1801e-01]], requires_grad=True)) ('2.bias', Parameter containing:\n",
      "tensor([ 0.2908, -0.2333, -0.2519, -0.1676, -0.1603, -0.0342, -0.2524, -0.2191],\n",
      "       requires_grad=True)) ('6.weight', Parameter containing:\n",
      "tensor([[ 0.3300,  0.2996, -0.1669, -0.0503,  0.1205, -0.1294, -0.1214, -0.1758]],\n",
      "       requires_grad=True)) ('6.bias', Parameter containing:\n",
      "tensor([-0.1714], requires_grad=True))\n"
     ]
    }
   ],
   "execution_count": 6
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
